package com.automapper.utils;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;

import org.apache.commons.lang3.StringUtils;

import com.automapper.exception.AutoMapperException;

public class FieldFacade {

	private Field field;
	private Object obj;
	
	public FieldFacade(Field field, Object obj) {
		super();
		this.field = field;
		this.obj = obj;
	}
	
	public String getColumnName() {
		Column column = field.getAnnotation(Column.class);
		if (column == null) {
			return "";
		} else {
			return StringUtils.isNotBlank(column.name()) ? column.name() : field.getName();
		}
	}
	
	public Field getField() {
		return field;
	}
	
	/**
	 * 获取对象的属性名称，封装Field.getName()方法
	 * @return 属性名
	 */
	public String getFieldName() {
		return field.getName();
	}
		
	public Class<?> getJavaType() {
		return field.getType();
	}
	
	/**
	 * 基本类型，int，long等于0，返回true
	 * @return
	 * @throws Exception 
	 */
	public boolean isNumEqNullOrEqZero() throws Exception {
		Object object = this.getFieldValue();
		String classType = this.getJavaType().getName();
		if (object == null) {
			return true;
		}
		if (("int".equalsIgnoreCase(classType) || (object instanceof Integer)) && ((Integer)object) == 0) {
			return true;
		}
		if (("long".equalsIgnoreCase(classType) || (object instanceof Long)) && ((Long)object) == 0) {
			return true;
		}
		return false;
	}
	
	/**
	 *  判断field的值是否为null，返回true
	 * @return
	 * @throws Exception 
	 */
	public boolean isBlank() throws Exception {
		Object object = this.getFieldValue();
		if (object == null) {
			return true;
		}
		return false;
	}
	
	public boolean isId() {
		if (field.getAnnotation(Id.class) != null) {
			return true;
		}
		return false;
	}
	
	public Object getFieldValue() throws Exception {
		return field.get(obj);
	}
	
	/**
	 * 是否有级联对象
	 * @return
	 */
	public boolean hasNestedResults() {
		OneToMany oneToMany = field.getAnnotation(OneToMany.class);
		if (oneToMany == null) {
			return false;
		}
		return true;
	}
	
	/**
	 * 获取泛型的对象类型
	 * @return
	 */
	public Class<?> getNestedResultsClassPath() {
		Class<?> classPath = null;
		OneToMany oneToMany = field.getAnnotation(OneToMany.class);
		if (oneToMany != null && oneToMany.targetEntity() != void.class) {
			classPath = oneToMany.targetEntity();
		} else {
			Class<?> clazz = this.getJavaType();
			if(clazz.isAssignableFrom(List.class)) {
				Type fc = this.getField().getGenericType(); // 关键的地方，如果是List类型，得到其Generic的类型
				ParameterizedType pt = (ParameterizedType) fc;
				classPath = (Class<?>) pt.getActualTypeArguments()[0]; //得到泛型里的class类型对象。
			}
		}
		return classPath;
	}
	
	public boolean isJoinColumn() {
		JoinColumn joinColumn = field.getAnnotation(JoinColumn.class);
		if (joinColumn == null) {
			return false;
		} else {
			return true;
		}
	}
	
	public String getJoinColumnName() {
		JoinColumn joinColumn = field.getAnnotation(JoinColumn.class);
		String name = joinColumn.name();
		AutoMapperException.StringisBlankRuntimeException(name, "@JoinColumn没有给name赋值");
		return name;
	}
	
	/**
	 * 是否允许为空，默认值是true
	 */
//	public boolean nullable() {
//		return field.getAnnotation(Column.class).nullable();
//	}
}
